package BuildTrainSet.AssignTag;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

/**
 *
 * @author phamthanh
 * Gán thẻ alpha cho file, những nsw nào là một từ trong từ điển thì bỏ thẻ nsw của nó.
 */
public final class OtherTag {

    //private ArrayList conAr = new ArrayList();
    public HashSet wordAr = new HashSet();
    
    public OtherTag(String inF, String ouF) throws ClassNotFoundException, SQLException, UnsupportedEncodingException, XMLStreamException, FileNotFoundException, IOException {
        System.out.println("package BuildTrainSet.AssignTag;class OtherTag, method OtherTag");
        createAlphaTagFile(inF, ouF);
    }
public static void main(String argv[]) throws ClassNotFoundException, SQLException, UnsupportedEncodingException, XMLStreamException, FileNotFoundException, IOException{
        OtherTag ot=new OtherTag("trainset//hand//alpha_hand.xml","trainset//trainset_alpha_hand_other.xml");
    }
    public void createAlphaTagFile(String inF, String ouF) throws XMLStreamException, IOException {
        long startTime = System.currentTimeMillis();

        FileInputStream fis = new FileInputStream(inF);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        FileOutputStream fos = new FileOutputStream(ouF);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
        XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(osw);
        xmlStreamWriter.writeStartElement("root");
        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {

                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("record")) {
                        String RecId = xmlStreamReader.getAttributeValue(0);
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeAttribute("RecId", RecId);
                    }
                    if (localName.equals("URL")) {
                        String url = xmlStreamReader.getElementText();

                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeCharacters(url);
                        xmlStreamWriter.writeEndElement();
                    }
                    if (localName.equals("Infomation")) {
                        xmlStreamWriter.writeStartElement(localName);
                    }

                    if (localName.equals("W")) {

                        String atVal = xmlStreamReader.getAttributeValue(0);
                        if (atVal.equals("OTHER")) {
                            String nsw = xmlStreamReader.getElementText();

                            String atOtVal = detectOtherTag(nsw);
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atOtVal);
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();

                        } else {
                            xmlStreamWriter.writeStartElement(localName);
                            xmlStreamWriter.writeAttribute("NSW", atVal);
                            String nsw = xmlStreamReader.getElementText();
                            xmlStreamWriter.writeCharacters(nsw);
                            xmlStreamWriter.writeEndElement();
                        }

                    }

                    if (localName.equals("SPLIT")) {
                        xmlStreamWriter.writeStartElement(localName);
                    }
                    break;


                case XMLStreamConstants.END_ELEMENT:
                    String endLocalName = xmlStreamReader.getLocalName();

                    if (endLocalName.equals("record")) {
                        xmlStreamWriter.writeEndElement();
                    }
                    if (endLocalName.equals("Infomation")) {
                        xmlStreamWriter.writeEndElement();
                    }

                    if (endLocalName.equals("SPLIT")) {
                        xmlStreamWriter.writeEndElement();
                    }

                    break;
                case XMLStreamConstants.CHARACTERS:
                    String character = xmlStreamReader.getText();
                    xmlStreamWriter.writeCharacters(character);
                    break;

                default:
                    break;
            }
        }
        xmlStreamWriter.writeEndElement();

        xmlStreamReader.close();
        xmlStreamWriter.close();
        fis.close();
        fos.close();

        long endTime = System.currentTimeMillis();
        long time = endTime - startTime;
        System.out.println("Thoi gian gan the other la: " + time);

    }

    public boolean isPunc(String nsw) {
//        Pattern PUNC = Pattern.compile("[\\(\\)\"\'\\{\\}");
        Pattern PUNC = Pattern.compile("[\\p{P}&]+");
        Matcher m = PUNC.matcher(nsw);
        return m.find();
    }

    public boolean isCseq(String nsw) {
        Pattern CSEQ = Pattern.compile("[\\p{P}]+");
        Matcher m = CSEQ.matcher(nsw);
        return m.find();
    }

    public String detectOtherTag(String nsw) {
        if (isPunc(nsw)) {
            return "PUNC";
        }

//        if (isCseq(nsw)) {
//            return "CSEQ";
//        }
        return "NONE";
    }
}
